var newsFeedRT;
var newsFeedTimeId;

function initNewsFeedIndex() {
    var delay = 5000;
    newsFeedTime = new Array();

    updateNewsFeedTime();

    newsFeedRT = setInterval(updateNewsFeedRT, delay);
    newsFeedTimeId = setInterval(updateNewsFeedTime, delay);
}

function moreNewsFeed() {
    window.event.preventDefault();

    var notifs = getElementByClass("notif", document.getElementById("notifs"));
    var oldestNotif = notifs[notifs.length - 1];
    var oldestNotifId = oldestNotif.id.replace("notif-", "");
    
    var ajax = new AjaxWrap();
    ajax.onLoadSuccess = function(ajax) {
        document.getElementById("notifs").innerHTML += ajax.xhr.responseText;
    }
    
    ajax.openAndSend("POST", URL_BASE + "newsfeed/index/" + oldestNotifId, true, "ajax=true&partial=true");
    
    return false;
}

function updateNewsFeedRT() {
    var notifs = getElementByClass("notif", document.getElementById("notifs"));
    var newestFeed = notifs[0];
    var newestFeedId;
    if (newestFeed) {
        newestFeedId = newestFeed.id.replace("notif-", "");
    }

    var ajax = new AjaxWrap();
    ajax.onLoadSuccess = function(ajax) {
        var notifs =document.getElementById("notifs");
        notifs.innerHTML = ajax.xhr.responseText + notifs.innerHTML;
    }
    
    var param = "";
    if (newestFeed) {
        param = newestFeedId;
    }
    ajax.openAndSend("POST", URL_BASE + "newsfeed/newestfeed/" + param, true, "ajax=true");
}

function updateNewsFeedTime() {
    var times = getElementByClass("time", document.getElementById("notifs"));
    var length = times.length;

    for (i=0; i < length; ++i) {
        var date = new Date();
        var t = times[i];
        if(newsFeedTime[t.id]) {
        //t.innerHTML = calculateTime(t.id.replace("time-", "") + (date.getTime() - newsFeedTime[t.id])/1000);
        } else {
            newsFeedTime[t.id] = date.getTime();
        }

        t.innerHTML = calculateTime(Number(t.id.replace("time-", "")) + Number((date.getTime() - newsFeedTime[t.id])/1000));
    }
}

function calculateTime(seconds) {
    if (seconds < 60) {
        if (seconds < 2) {
            return Math.floor(seconds) + " second ago";
        } else {
            return Math.floor(seconds) + " seconds ago";
        }
    } else if (seconds < 60 * 60) {
        minutes = seconds / 60;
        if (minutes < 2) {
            return Math.floor(minutes) + " minute ago";
        } else {
            return Math.floor(minutes) + " minutes ago";
        }
    } else if (seconds < 60 * 60 * 24) {
        hours = seconds / 60 / 60;
        if (hours < 2) {
            return Math.floor(hours) + " hour ago";
        } else {
            return Math.floor(hours) + " hours ago";
        }
    } else {
        days = seconds / 60 / 60 / 24;
        if (days < 2) {
            return Math.floor(days) + " day ago";
        } else {
            return Math.floor(days) + " days ago";
        }
    }
}

function closeNewsFeedIndex() {
    // Stop
    if(newsFeedRT != null){
        clearInterval(newsFeedRT);
    }
    if (newsFeedTimeId != null) {
        clearInterval(newsFeedTimeId);
    }
}